import java.util.ArrayList;

public class BetterAgent extends Agent{

	public final int NUMBER_SAMPLES = 5000;
	public int RAISE_VALUE = 5;
	public int HIGH_RAISE_VALUE = 10;

	@Override
	public void evaluateProbabilities()
	{
		
	}
	
	
	@Override
	public Action takeTurn()
	{
		printCards();
		
		
		int currentBet = table.getMaxBet();
		//gets estimates for me
		int myValue = getMyEstimates(table);
		
		int oppValue = getOpponentEstimates(table);
		
		int diff = myValue - oppValue;
		//System.out.println("Difference: "+diff);
		
		if (diff < -1000000)
			return Action.foldAction();
		if (diff > 1000000)
			return Action.raiseAction(currentBet + HIGH_RAISE_VALUE);
		else if (diff < 0)
			return Action.callAction();
		else
			return Action.raiseAction(currentBet + RAISE_VALUE);
		
			
	}
	
	public int getMyEstimates(Table table)
	{
		long avg = 0;
		for (int i = 0; i < NUMBER_SAMPLES; i++)
		{
			ArrayList<Card> deck = table.getCards(this);
			ArrayList<Card> potentialHand = cloneCards(cards);
			potentialHand.addAll(cloneCards(table.visibleCards));

			for (int j = potentialHand.size(); j < 7; j++)
				potentialHand.add(deck.remove(0));

			Hand best = Hand.getBestHand(potentialHand);
			avg += best.getValue();
		}
		
		int results = (int)(avg / (double)NUMBER_SAMPLES);
		return results;

	}
	
	public int getOpponentEstimates(Table table)
	{
		long avg = 0;
		for (int i = 0; i < NUMBER_SAMPLES; i++)
		{
			ArrayList<Card> deck = table.getCards(this);
			ArrayList<Card> potentialHand = new ArrayList<Card>();
			potentialHand.addAll(cloneCards(table.visibleCards));

			for (int j = potentialHand.size(); j < 7; j++)
				potentialHand.add(deck.remove(0));

			Hand best = Hand.getBestHand(potentialHand);
			avg += best.getValue();
		}
		
		int results = (int)(avg / (double)NUMBER_SAMPLES);
		return results;

	}

	public void simulate(Table table)
	{
		Integer[] handArray = new Integer[9];
		for (int i = 0; i < 9; i++)
			handArray[i] = 0;
		for (int i = 0; i < NUMBER_SAMPLES; i++)
		{
			ArrayList<Card> deck = table.getCards(this);
			ArrayList<Card> potentialHand = cloneCards(cards);
			potentialHand.addAll(cloneCards(table.visibleCards));

			for (int j = potentialHand.size(); j < 7; j++)
				potentialHand.add(deck.remove(0));

			Hand best = Hand.getBestHand(potentialHand);
			handArray[best.getType()]++;
		}
		
		System.out.println("Samples Results:");
		String results = "";
		for (int i = 0; i<9; i++ )
			results += ""+handArray[i]+", ";

		System.out.println(results);
	}

	public ArrayList<Card> cloneCards(ArrayList<Card> hand)
	{
		ArrayList<Card> cloneHand = new ArrayList<Card>();
		for (Card c:hand)
		{
			cloneHand.add(c.clone());
		}
		return cloneHand;
	}

}
